#Import Modules
import os, pygame, math
from pygame.locals import *

from PhysicsObject import *

class Projectile(PhysicsObject):
    """A class required by all sprites that expect to move or run through the physics engine."""
    def __init__(self,spawnpoint,collide):
        PhysicsObject.__init__(self,spawnpoint)
        # This class decribes the basic projectile.

        self.accelerationy = self.gravity
        
        
        # Collide
        if collide[0] == False and collide[1] == False:
            self.walls = None
        elif collide[0] == False and collide[1] == True:
            self.walls = PhysicsObject.ground
        elif collide[0] == True and collide[1] == False:
            self.walls = PhysicsObject.wall
        elif collide[0] == True and collide[1] == True:
            self.walls = PhysicsObject.unified
    
    def hit(self, damage, stun=10, direction=-1, knockback=5):
        if self.invincible == 0:
            if direction != -1:
                self.speedx += knockback * math.cos(direction)
                self.speedy -= knockback * math.sin(direction)
        PhysicsObject.hit(self, damage, stun, direction, knockback)
    
    # Don't bother unless we find a reason to override.
    #def update(self):
    #    PhysicsObject.update(self)
        
    def physics(self):
        
        PhysicsObject.physics(self)
        
        if self.walls != None:
            
            # We do this conversion twice, but it's ok, that's the price you pay for clean object orientation.
            totalforce = math.pow(math.pow(self.speedx,2)+math.pow(self.speedy,2),0.5)
            if totalforce == 0:
                angle = -math.pi/2
            else:
                angle = math.atan2(-self.speedy, self.speedx)
            
            # Check for wall collisions
            hitangle = (collisionang(self, self.walls)) # Calculate the collision normal
            if hitangle != 5:
                
                newposx = self.positionx
                newposy = self.positiony
                
                # Initialize character event flags
                oldposx = self.lastpositionx
                oldposy = self.lastpositiony
                
                if abs(newposx-oldposx) > abs(newposy-oldposy):
                    crawlx = cmp((newposx-oldposx),0)
                    crawly = ((newposy-oldposy)/abs(newposx-oldposx))
                elif abs(newposx-oldposx) < abs(newposy-oldposy):
                    crawly = cmp((newposy-oldposy),0)
                    crawlx = ((newposx-oldposx)/abs(newposy-oldposy))
                elif (newposx != oldposx):
                    crawlx = cmp((newposx-oldposx),0)
                    crawly = cmp((newposy-oldposy),0)
                else:
                    print "Collision Error: Hit wall without moving!"
                
                self.positionx = oldposx
                self.positiony = oldposy
                
                if math.floor(self.positionx) != math.floor(newposx):
                    xgo = True
                else:
                    xgo = False
                if math.floor(self.positiony) != math.floor(newposy):
                    ygo = True
                else:
                    ygo = False
                
                while xgo or ygo:
                    if xgo:
                        self.positionx = self.positionx + crawlx
                        if collision(self,PhysicsObject.wall):
                            self.positionx = self.positionx - crawlx
                            xgo = False
                        elif math.floor(self.positionx) == math.floor(newposx):
                            self.positionx = newposx
                            xgo = False
                    if ygo:
                        self.positiony = self.positiony + crawly
                        if collision(self,PhysicsObject.wall):
                            self.positiony = self.positiony - crawly
                            ygo = False
                        elif math.floor(self.positiony) == math.floor(newposy):
                            self.positiony = newposy
                            ygo = False
                            
                self.lastpositionx = self.positionx
                self.lastpositiony = self.positiony
                
                angle = 2*hitangle - (angle + math.pi)
                totalforce = self.elasticity * totalforce
            
            # Cnvert back from polar
            self.speedy = totalforce * -math.sin(angle)
            self.speedx = totalforce * math.cos(angle)
            
            # Store current position for next time
            self.lastpositionx = self.positionx
            self.lastpositiony = self.positiony